home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / GETARG.C < prev    next >
C/C++ Source or Header  |  1990-09-06  |  26KB  |  633 lines

  1. /***************************************************************************
  2. *  Routines to grab the    parameters from    the command line :           *
  3. * All the routines except the main one,    starts with GA (Get Arguments) to  *
  4. * prevent from names conflicts.                           *
  5. * It is    assumed    in these routine that any pointer, for any type    has the       *
  6. * same length (i.e. length of int pointer is equal to char pointer etc.)   *
  7. *                                       *
  8. *  The following routines are available    in this    module:               *
  9. * 1. int GAGetArgs(argc, argv, CtrlStr, Variables...)               *
  10. *    where argc, argv as received on entry.                   *
  11. *       CtrlStr is the contrl string    (see below)               *
  12. *       Variables are all the variables to be set according to CtrlStr. *
  13. *       Note    that all the variables MUST be transfered by address.       *
  14. *    return 0 on correct parsing, otherwise error number (see GetArg.h).   *
  15. * 2. GAPrintHowTo(CtrlStr)                           *
  16. *    Print the control string to stderr, in the    correct    format needed.       *
  17. *    This feature is very useful in case of error during GetArgs parsing.  *
  18. *    Chars equal to SPACE_CHAR are not printed (regular spaces are NOT     *
  19. *    allowed, and so using SPACE_CHAR you can create space in PrintHowTo). *
  20. * 3. GAPrintErrMsg(Error)                           *
  21. *    Print the error to    stderr,    according to Error (usually returned by       *
  22. *    GAGetArgs).                               *
  23. *                                       *
  24. *     CtrlStr format:                               *
  25. *   The    control    string passed to GetArgs controls the way argv (argc) are  *
  26. * parsed. Each entry in    this string must not have any spaces in    it. The       *
  27. * First    Entry is the name of the program which is usually ignored except   *
  28. * when GAPrintHowTo is called. All the other entries (except the last one  *
  29. * which    we will    come back to it    later) must have the following format:       *
  30. * 1. One letter    which sets the option letter.                   *
  31. * 2. '!' or '%'    to determines if this option is    really optional    ('%') or   *
  32. *    it    must exists ('!')...                           *
  33. * 3. '-' allways.                               *
  34. * 4. Alpha numeric string, usually ignored, but    used by    GAPrintHowTo to       *
  35. *    print the meaning of this input.                       *
  36. * 5. Sequences starts with '!' or '%'. Again if    '!' then this sequence       *
  37. *    must exists (only if its option flag is given of course), and if '%'  *
  38. *    it    is optional. Each sequence will    continue with one or two       *
  39. *    characters    which defines the kind of the input:               *
  40. *    a.    d, x, o, u - integer is expected (decimal, hex, octal base or       *
  41. *          unsigned).                           *
  42. *    b.    D, X, O, U - long integer is expected (same as above).           *
  43. *    c.    f    - float    number is expected.                   *
  44. *    d.    F    - double number    is expected.                   *
  45. *    e.    s    - string is expected.                       *
  46. *    f.    *?    - any number of    '?' kind (d, x, o, u, D, X, O, U, f, F, s) *
  47. *          will match this one. If '?' is numeric, it scans until   *
  48. *          none numeric input is given. If '?' is 's' then it scans *
  49. *          up to the next option or end of argv.               *
  50. *                                       *
  51. *   If the last    parameter given    in the CtrlStr,    is not an option (i.e. the *
  52. * second char is not in    ['!', '%'] and the third one is not '-'), all what *
  53. * remained from    argv is    linked to it.                       *
  54. *                                       *
  55. *   The    variables passed to GAGetArgs (starting    from 4th parameter) MUST   *
  56. * match    the order of the CtrlStr:                       *
  57. *   For    each option, one integer address must be passed. This integer must *
  58. * initialized by 0. If that option is given in the command line, it will   *
  59. * be set to one.                               *
  60. *   In addition, the sequences that might follow an option require the       *
  61. * following parameters to pass:                           *
  62. * 1. d, x, o, u - pointer to integer (int *).                   *
  63. * 2. D, X, O, U - pointer to long (long *).                   *
  64. * 3. f         - pointer to float      (float *).                   *
  65. * 4. F         - pointer to double  (double *).                   *
  66. * 5. s         - pointer to char      (char    *). NO allocation is needed!       *
  67. * 6. *?         - TWO variables are passed    for each wild request. the first   *
  68. *           one is (address of) integer, and    it will    return number of   *
  69. *           parameters actually matched this    sequence, and the second   *
  70. *           one is a    pointer    to pointer to ?    (? **),    and will return    an *
  71. *           address to a block of pointers to ? kind, terminated with   *
  72. *           NULL pointer. NO    pre-allocation is needed.           *
  73. *           note that these two variables are pretty    like the argv/argc *
  74. *           pair...                               *
  75. *                                       *
  76. *   Examples:                                   *
  77. *                                       *
  78. *    "Example1  i%-OneInteger!d  s%-Strings!*s  j%-  k!-Float!f  Files"       *
  79. * Will match: Example1 -i 77 -s    String1    String2    String3    -k 88.2    File1 File2*
  80. *   or match: Example1 -s String1 -k 88.3 -i 999 -j               *
  81. *    but not: Example1 -i 77 78    (option    i expects one integer, k must be). *
  82. * Note the option k must exists, and that the order of the options is not  *
  83. * not important. In the    first examples File1 & File2 will match    the Files  *
  84. * in the command line.                               *
  85. * A call to GAPrintHowTo with this CtrlStr will    print to stderr:       *
  86. * Example1 [-i OneIngeter] [-s Strings...] [-j]    -k Float Files...       *
  87. *                                       *
  88. *   Notes:                                   *
  89. *                                       *
  90. * 1. This module assumes that all the pointers to all kind of data types   *
  91. *    have the same length and format, i.e. sizeof(int *) == sizeof(char    *).*
  92. *                                       *
  93. *                      Gershon Elber    Ver 0.2     Mar 88       *
  94. ****************************************************************************
  95. * History:                                   *
  96. * 11 Mar 88 - Version 1.0 by Gershon Elber.                   *
  97. ***************************************************************************/
  98.  
  99. #ifdef __MSDOS__
  100. #include <stdlib.h>
  101. #include <alloc.h>
  102. #endif /* __MSDOS__ */
  103.  
  104. #ifdef USE_VARARGS
  105. #include <varargs.h>
  106. #endif /* USE_VARARGS */
  107.  
  108. #include <stdio.h>
  109. #include <string.h>
  110. #include "getarg.h"
  111.  
  112. #define    MYMALLOC       /* If no "MyAlloc" routine elsewhere define this. */
  113.  
  114. #define    MAX_PARAM    100        /* maximum number of parameters allowed. */
  115. #define    CTRL_STR_MAX_LEN    1024
  116.  
  117. #define SPACE_CHAR    '|'      /* The character not to print using HowTo. */
  118.  
  119. #ifndef    TRUE
  120. #define    TRUE -1
  121. #define    FALSE 0
  122. #endif /* TRUE */
  123.  
  124. #define    ARG_OK    0
  125.  
  126. #define    ISSPACE(x) ((x)    <= ' ')           /* Not conventional - but works fine! */
  127. /* The two characters '%' and '!' are used in the control string: */
  128. #define    ISCTRLCHAR(x) (((x) == '%') || ((x) == '!'))
  129.  
  130. static char *GAErrorToken;/* On error code, ErrorToken is set to point on it.*/
  131.  
  132. static int GATestAllSatis(char *CtrlStrCopy, char *CtrlStr, int *argc,
  133.     char ***argv, int *Parameters[MAX_PARAM], int *ParamCount);
  134. static int GAUpdateParameters(int *Parameters[], int *ParamCount,
  135.     char *Option, char *CtrlStrCopy, char *CtrlStr, int *argc,
  136.     char ***argv);
  137. static int GAGetParmeters(int *Parameters[], int *ParamCount,
  138.     char *CtrlStrCopy , char *Option, int *argc, char ***argv);
  139. static int GAGetMultiParmeters(int *Parameters[], int *ParamCount,
  140.     char *CtrlStrCopy, int *argc, char ***argv);
  141. static void GASetParamCount(char *CtrlStr, int Max, int *ParamCount);
  142. static void GAByteCopy(char *Dst, char *Src, unsigned n);
  143. static int GAOptionExists(int argc, char **argv);
  144. #ifdef    MYMALLOC
  145. static char *MyMalloc(unsigned size);
  146. #endif    /* MYMALLOC */
  147.  
  148. /***************************************************************************
  149. * Routine to access the    command    line argument and interpret them:       *
  150. * Return ARG_OK (0) is case of succesfull parsing, error code else...       *
  151. ***************************************************************************/
  152. #ifdef USE_VARARGS
  153. int GAGetArgs(int va_alist, ...)
  154. {
  155.     va_list ap;
  156.     int argc, i, Error = FALSE, ParamCount = 0,
  157.     *Parameters[MAX_PARAM];           /* Save here parameter addresses. */
  158.     char **argv, *CtrlStr, *Option, CtrlStrCopy[CTRL_STR_MAX_LEN];
  159.  
  160.     va_start(ap);
  161.  
  162.     argc = va_arg(ap, int);
  163.     argv = va_arg(ap, char **);
  164.     CtrlStr = va_arg(ap, char *);
  165.  
  166.     va_end(ap);
  167.  
  168.     strcpy(CtrlStrCopy, CtrlStr);
  169.  
  170.     /* Using base address of parameters we access other parameters addr:  */
  171.     /* Note that me (for sure!) samples data beyond the current function  */
  172.     /* frame, but we accesson these set address only by demand.          */
  173.     for (i = 1; i <= MAX_PARAM; i++) Parameters[i-1] = va_arg(ap, int *);
  174. #else
  175. int GAGetArgs(int argc, char **argv, char *CtrlStr, ...)
  176. {
  177.     int i, Error = FALSE, ParamCount = 0,
  178.     *Parameters[MAX_PARAM];           /* Save here parameter addresses. */
  179.     char *Option, CtrlStrCopy[CTRL_STR_MAX_LEN];
  180.  
  181.     strcpy(CtrlStrCopy, CtrlStr);
  182.  
  183.     /* Using base address of parameters we access other parameters addr:  */
  184.     /* Note that me (for sure!) samples data beyond the current function  */
  185.     /* frame, but we accesson these set address only by demand.          */
  186.     for (i = 1; i <= MAX_PARAM; i++) Parameters[i-1] = (int *) *(i + &CtrlStr);
  187. #endif /* USE_VARARG */
  188.  
  189.     --argc; argv++;        /* Skip the program name (first in argv/c list). */
  190.     while (argc >= 0) {
  191.     if (!GAOptionExists(argc, argv)) break;            /* The loop. */
  192.     argc--;
  193.     Option    = *argv++;
  194.     if ((Error = GAUpdateParameters(Parameters, &ParamCount, Option,
  195.          CtrlStrCopy, CtrlStr, &argc, &argv)) != FALSE) return Error;
  196.     }
  197.     /*    Check for results and update trail of command line: */
  198.     return GATestAllSatis(CtrlStrCopy, CtrlStr, &argc, &argv, Parameters,
  199.                                  &ParamCount);
  200. }
  201.  
  202. /***************************************************************************
  203. * Routine to search for    unsatisfied flags - simply scan    the list for !-       *
  204. * sequence. Before this    scan, this routine updates the rest of the command *
  205. * line into the    last two parameters if it is requested by the CtrlStr       *
  206. * (last    item in    CtrlStr    is NOT an option).                   *
  207. * Return ARG_OK if all satisfied, CMD_ERR_AllSatis error else.           *
  208. ***************************************************************************/
  209. static int GATestAllSatis(char *CtrlStrCopy, char *CtrlStr, int *argc,
  210.     char ***argv, int *Parameters[MAX_PARAM], int *ParamCount)
  211. {
  212.     int    i;
  213.     static char    *LocalToken = NULL;
  214.  
  215.     /* If LocalToken is not initialized - do it now. Note that this string */
  216.     /* should be writable as well so we can not assign it directly.        */
  217.     if (LocalToken == NULL) {
  218.         LocalToken = (char *) malloc(3);
  219.     strcpy(LocalToken, "-?");
  220.     }
  221.  
  222.     /* Check is    last item is an    option.    If not then copy rest of command */
  223.     /* line into it as 1. NumOfprm, 2. pointer to block    of pointers.     */
  224.     for (i = strlen(CtrlStr) - 1; i > 0 && !ISSPACE(CtrlStr[i]); i--);
  225.     if (!ISCTRLCHAR(CtrlStr[i + 2])) {
  226.     GASetParamCount(CtrlStr, i, ParamCount);   /* Point in correct prm.. */
  227.     *Parameters[(*ParamCount)++] = *argc;
  228.     GAByteCopy((char *) Parameters[(*ParamCount)++], (char *) argv,
  229.                             sizeof(char *));
  230.     }
  231.  
  232.     i =    0;
  233.     while (++i < strlen(CtrlStrCopy))
  234.     if ((CtrlStrCopy[i] == '-') && (CtrlStrCopy[i-1] == '!')) {
  235.         GAErrorToken = LocalToken;
  236.         LocalToken[1] = CtrlStrCopy[i-2];        /* Set the corrent flag. */
  237.         return CMD_ERR_AllSatis;
  238.     }
  239.  
  240.     return ARG_OK;
  241. }
  242.  
  243. /***************************************************************************
  244. * Routine to update the    parameters according to    the given Option:       *
  245. ***************************************************************************/
  246. static int GAUpdateParameters(int *Parameters[], int *ParamCount,
  247.        char *Option, char *CtrlStrCopy, char *CtrlStr, int *argc, char ***argv)
  248. {
  249.     int    i, BooleanTrue = Option[2] != '-';
  250.  
  251.     if (Option[0] != '-') {
  252.     GAErrorToken = Option;
  253.     return CMD_ERR_NotAnOpt;
  254.     }
  255.     i =    0;                /* Scan the CtrlStrCopy for that option: */
  256.     while (i + 2 < strlen(CtrlStrCopy)) {
  257.     if ((CtrlStrCopy[i] == Option[1]) && (ISCTRLCHAR(CtrlStrCopy[i + 1]))
  258.         && (CtrlStrCopy[i+2] == '-')) {
  259.         /* We found    that option! */
  260.         break;
  261.     }
  262.     i++;
  263.     }
  264.     if (i + 2 >= strlen(CtrlStrCopy)) {
  265.     GAErrorToken = Option;
  266.     return CMD_ERR_NoSuchOpt;
  267.     }
  268.  
  269.     /* If we are here, then we found that option in CtrlStr - Strip it off:  */
  270.     CtrlStrCopy[i] = CtrlStrCopy[i + 1] = CtrlStrCopy[i + 2] = (char) ' ';
  271.     GASetParamCount(CtrlStr, i, ParamCount);/*Set it to point in correct prm.*/
  272.     i += 3;
  273.     /* Set boolean flag for that option. */
  274.     *Parameters[(*ParamCount)++] = BooleanTrue;
  275.     if (ISSPACE(CtrlStrCopy[i]))
  276.     return ARG_OK;               /* Only a boolean flag is needed. */
  277.  
  278.     /* Skip the    text between the boolean option and data follows: */
  279.     while (!ISCTRLCHAR(CtrlStrCopy[i]))    i++;
  280.     /* Get the parameters and return the appropriete return code: */
  281.     return GAGetParmeters(Parameters, ParamCount, &CtrlStrCopy[i],
  282.                               Option, argc, argv);
  283. }
  284.  
  285. /***************************************************************************
  286. * Routine to get parameters according to the CtrlStr given from    argv/c :   *
  287. ***************************************************************************/
  288. static int GAGetParmeters(int *Parameters[], int *ParamCount,
  289.     char *CtrlStrCopy , char *Option, int *argc, char ***argv)
  290. {
  291.     int    i = 0, ScanRes;
  292.  
  293.     while (!(ISSPACE(CtrlStrCopy[i]))) {
  294.     switch (CtrlStrCopy[i+1]) {
  295.         case 'd':                     /* Get signed integers. */
  296.         ScanRes    = sscanf(*((*argv)++), "%d",
  297.                      (int *) Parameters[(*ParamCount)++]);
  298.         break;
  299.         case 'u':                   /* Get unsigned integers. */
  300.         ScanRes    = sscanf(*((*argv)++), "%u",
  301.                     (unsigned *) Parameters[(*ParamCount)++]);
  302.         break;
  303.         case 'x':                    /* Get hex integers. */
  304.         ScanRes    = sscanf(*((*argv)++), "%x",
  305.                      (int *) Parameters[(*ParamCount)++]);
  306.         break;
  307.         case 'o':                      /* Get octal integers. */
  308.         ScanRes    = sscanf(*((*argv)++), "%o",
  309.                      (int *) Parameters[(*ParamCount)++]);
  310.         break;
  311.         case 'D':                /* Get signed long integers. */
  312.         ScanRes    = sscanf(*((*argv)++), "%ld",
  313.                     (long *) Parameters[(*ParamCount)++]);
  314.         break;
  315.         case 'U':                  /* Get unsigned long integers. */
  316.         ScanRes    = sscanf(*((*argv)++), "%lu",
  317.                    (unsigned long *) Parameters[(*ParamCount)++]);
  318.         break;
  319.         case 'X':                   /* Get hex long integers. */
  320.         ScanRes    = sscanf(*((*argv)++), "%lx",
  321.                     (long *) Parameters[(*ParamCount)++]);
  322.         break;
  323.         case 'O':                 /* Get octal long integers. */
  324.         ScanRes    = sscanf(*((*argv)++), "%lo",
  325.                     (long *) Parameters[(*ParamCount)++]);
  326.         break;
  327.         case 'f':                    /* Get float number. */
  328.         ScanRes    = sscanf(*((*argv)++), "%f",
  329.                        (float *) Parameters[(*ParamCount)++]);
  330.         case 'F':                 /* Get double float number. */
  331.         ScanRes    = sscanf(*((*argv)++), "%lf",
  332.                       (double *) Parameters[(*ParamCount)++]);
  333.         break;
  334.         case 's':                      /* It as a string. */
  335.         ScanRes    = 1;                     /* Allways O.K. */
  336.         GAByteCopy((char *) Parameters[(*ParamCount)++],
  337.                     (char *) ((*argv)++), sizeof(char *));
  338.         break;
  339.         case '*':                 /* Get few parameters into one: */
  340.         ScanRes    = GAGetMultiParmeters(Parameters, ParamCount,
  341.                           &CtrlStrCopy[i], argc, argv);
  342.         if ((ScanRes ==    0) && (CtrlStrCopy[i] == '!')) {
  343.             GAErrorToken = Option;
  344.             return CMD_ERR_WildEmpty;
  345.         }
  346.         break;
  347.         default:
  348.         ScanRes = 0;               /* Make optimizer warning silent. */
  349.     }
  350.     /* If reading fails and    this number is a must (!) then error: */
  351.     if ((ScanRes ==    0) && (CtrlStrCopy[i] == '!')) {
  352.         GAErrorToken = Option;
  353.         return CMD_ERR_NumRead;
  354.     }
  355.     if (CtrlStrCopy[i+1] !=    '*') {
  356.         (*argc)--;         /* Everything is OK - update to next parameter: */
  357.         i += 2;             /* Skip to next parameter (if any). */
  358.     }
  359.     else
  360.         i += 3;                       /* Skip the '*' also! */
  361.     }
  362.  
  363.     return ARG_OK;
  364. }
  365.  
  366. /***************************************************************************
  367. * Routine to get few parameters    into one pointer such that the returned       *
  368. * pointer actually points on a block of    pointers to the    parameters...       *
  369. * For example *F means a pointer to pointers on    floats.               *
  370. * Returns number of parameters actually    read.                   *
  371. * This routine assumes that all    pointers (on any kind of scalar) has the   *
  372. * same size (and the union below is totally ovelapped bteween dif. arrays) *
  373. ***************************************************************************/
  374. static int GAGetMultiParmeters(int *Parameters[], int *ParamCount,
  375.     char *CtrlStrCopy, int *argc, char ***argv)
  376. {
  377.     int    i = 0, ScanRes,    NumOfPrm = 0, **Pmain, **Ptemp;
  378.     union TmpArray {    /* Save here the temporary data before copying it to */
  379.     int    *IntArray[MAX_PARAM];          /* the returned pointer block. */
  380.     long   *LngArray[MAX_PARAM];
  381.     float  *FltArray[MAX_PARAM];
  382.     double *DblArray[MAX_PARAM];
  383.     char   *ChrArray[MAX_PARAM];
  384.     } TmpArray;
  385.  
  386.     do {
  387.     switch(CtrlStrCopy[2]) {    /* CtrlStr == '!*?' or '%*?' where ? is. */
  388.         case 'd':               /* Format to read the parameters: */
  389.         TmpArray.IntArray[NumOfPrm] = (int *) MyMalloc(sizeof(int));
  390.         ScanRes    = sscanf(*((*argv)++), "%d",
  391.                        (int *) TmpArray.IntArray[NumOfPrm++]);
  392.         break;
  393.         case 'u':
  394.         TmpArray.IntArray[NumOfPrm] = (int *) MyMalloc(sizeof(int));
  395.         ScanRes    = sscanf(*((*argv)++), "%u",
  396.                   (unsigned    int *) TmpArray.IntArray[NumOfPrm++]);
  397.         break;
  398.         case 'o':
  399.         TmpArray.IntArray[NumOfPrm] = (int *) MyMalloc(sizeof(int));
  400.         ScanRes    = sscanf(*((*argv)++), "%o",
  401.                        (int *) TmpArray.IntArray[NumOfPrm++]);
  402.         break;
  403.         case 'x':
  404.         TmpArray.IntArray[NumOfPrm] = (int *) MyMalloc(sizeof(int));
  405.         ScanRes    = sscanf(*((*argv)++), "%x",
  406.                        (int *) TmpArray.IntArray[NumOfPrm++]);
  407.         break;
  408.         case 'D':
  409.         TmpArray.LngArray[NumOfPrm] = (long *) MyMalloc(sizeof(long));
  410.         ScanRes    = sscanf(*((*argv)++), "%ld",
  411.                       (long *) TmpArray.IntArray[NumOfPrm++]);
  412.         break;
  413.         case 'U':
  414.         TmpArray.LngArray[NumOfPrm] = (long *) MyMalloc(sizeof(long));
  415.         ScanRes    = sscanf(*((*argv)++), "%lu",
  416.                  (unsigned long *) TmpArray.IntArray[NumOfPrm++]);
  417.         break;
  418.         case 'O':
  419.         TmpArray.LngArray[NumOfPrm] = (long *) MyMalloc(sizeof(long));
  420.         ScanRes    = sscanf(*((*argv)++), "%lo",
  421.                       (long *) TmpArray.IntArray[NumOfPrm++]);
  422.         break;
  423.         case 'X':
  424.         TmpArray.LngArray[NumOfPrm] = (long *) MyMalloc(sizeof(long));
  425.         ScanRes    = sscanf(*((*argv)++), "%lx",
  426.                       (long *) TmpArray.IntArray[NumOfPrm++]);
  427.         break;
  428.         case 'f':
  429.         TmpArray.FltArray[NumOfPrm] = (float *)    MyMalloc(sizeof(float));
  430.         ScanRes    = sscanf(*((*argv)++), "%f",
  431.                      (float *) TmpArray.LngArray[NumOfPrm++]);
  432.         break;
  433.         case 'F':
  434.         TmpArray.DblArray[NumOfPrm] =
  435.                        (double *) MyMalloc(sizeof(double));
  436.         ScanRes    = sscanf(*((*argv)++), "%lf",
  437.                     (double *) TmpArray.LngArray[NumOfPrm++]);
  438.         break;
  439.         case 's':
  440.         while ((*argc) && ((**argv)[0] != '-'))    {
  441.             TmpArray.ChrArray[NumOfPrm++] = *((*argv)++);
  442.             (*argc)--;
  443.         }
  444.         ScanRes    = 0;               /* Force quit from do - loop. */
  445.         NumOfPrm++;        /* Updated again immediately after loop! */
  446.         (*argv)++;                           /* "" */
  447.         break;
  448.         default:
  449.         ScanRes = 0;               /* Make optimizer warning silent. */
  450.     }
  451.     (*argc)--;
  452.     }
  453.     while (ScanRes == 1);          /* Exactly one parameter was read. */
  454.     (*argv)--; NumOfPrm--; (*argc)++;
  455.  
  456.     /* Now allocate the    block with the exact size, and set it: */
  457.     Ptemp = Pmain = (int **) MyMalloc((unsigned) (NumOfPrm+1) *    sizeof(int *));
  458.     /* And here    we use the assumption that all pointers    are the    same: */
  459.     for (i = 0; i < NumOfPrm; i++)
  460.     *Ptemp++ = TmpArray.IntArray[i];
  461.     *Ptemp = NULL;               /* Close the block with NULL pointer. */
  462.  
  463.     /* That it save the    number of parameters read as first parameter to    */
  464.     /* return and the pointer to the block as second, and return:    */
  465.     *Parameters[(*ParamCount)++] = NumOfPrm;
  466.     GAByteCopy((char *)    Parameters[(*ParamCount)++], (char *) &Pmain,
  467.                                  sizeof(char *));
  468.     return NumOfPrm;
  469. }
  470.  
  471. /***************************************************************************
  472. * Routine to scan the CtrlStr, upto Max    and count the number of    parameters *
  473. * to that point:                               *
  474. * 1. Each option is counted as one parameter - boolean variable    (int)       *
  475. * 2. Within an option, each %? or !? is    counted    once - pointer to something*
  476. * 3. Within an option, %*? or !*? is counted twice - one for item count       *
  477. *    and one for pointer to block pointers.                   *
  478. * Note ALL variables are passed    by address and so of fixed size    (address). *
  479. ***************************************************************************/
  480. static void GASetParamCount(char *CtrlStr, int Max, int *ParamCount)
  481. {
  482.     int    i;
  483.  
  484.     *ParamCount    = 0;
  485.     for (i = 0; i < Max; i++) if (ISCTRLCHAR(CtrlStr[i])) {
  486.     if (CtrlStr[i+1] == '*')
  487.         *ParamCount += 2;
  488.     else
  489.         (*ParamCount)++;
  490.     }
  491. }
  492.  
  493. /***************************************************************************
  494. * Routine to copy exactly n bytes from Src to Dst. Note system library       *
  495. * routine strncpy should do the    same, but it stops on NULL char    !       *
  496. ***************************************************************************/
  497. static void GAByteCopy(char *Dst, char *Src, unsigned n)
  498. {
  499.     while (n--)    *(Dst++) = *(Src++);
  500. }
  501.  
  502. /***************************************************************************
  503. * Routine to check if more option (i.e.    first char == '-') exists in the   *
  504. * given    list argc, argv:                           *
  505. ***************************************************************************/
  506. static int GAOptionExists(int argc, char **argv)
  507. {
  508.     while (argc--)
  509.     if ((*argv++)[0] == '-') return TRUE;
  510.     return FALSE;
  511. }
  512.  
  513. /***************************************************************************
  514. * Routine to print some    error messages,    for this module:           *
  515. ***************************************************************************/
  516. void GAPrintErrMsg(int Error)
  517. {
  518.     fprintf(stderr, "Error in command line parsing - ");
  519.     switch (Error) {
  520.     case 0:;
  521.         fprintf(stderr, "Undefined error");
  522.         break;
  523.     case CMD_ERR_NotAnOpt:
  524.         fprintf(stderr, "None option Found");
  525.         break;
  526.     case CMD_ERR_NoSuchOpt:
  527.         fprintf(stderr, "Undefined option Found");
  528.         break;
  529.     case CMD_ERR_WildEmpty:
  530.         fprintf(stderr, "Empty input for '!*?' seq.");
  531.         break;
  532.     case CMD_ERR_NumRead:
  533.         fprintf(stderr, "Failed on reading number");
  534.         break;
  535.     case CMD_ERR_AllSatis:
  536.         fprintf(stderr, "Fail to satisfy");
  537.         break;
  538.     }
  539.     fprintf(stderr, " - '%s'.\n", GAErrorToken);
  540. }
  541.  
  542. /***************************************************************************
  543. * Routine to print correct format of command line allowed:           *
  544. ***************************************************************************/
  545. void GAPrintHowTo(char *CtrlStr)
  546. {
  547.     int    i = 0, SpaceFlag;
  548.  
  549.     fprintf(stderr, "Usage: ");
  550.     /* Print program name - first word in ctrl.    str. (optional): */
  551.     while (!(ISSPACE(CtrlStr[i])) && (!ISCTRLCHAR(CtrlStr[i+1])))
  552.     fprintf(stderr, "%c", CtrlStr[i++]);
  553.  
  554.     while (i < strlen(CtrlStr))    {
  555.     while ((ISSPACE(CtrlStr[i])) &&    (i < strlen(CtrlStr))) i++;
  556.     switch (CtrlStr[i+1]) {
  557.         case '%':
  558.         fprintf(stderr, " [-%c", CtrlStr[i++]);
  559.         i += 2;             /* Skip the '%-' or '!- after the char! */
  560.         SpaceFlag = TRUE;
  561.         while (!ISCTRLCHAR(CtrlStr[i]) && (i < strlen(CtrlStr))    &&
  562.             (!ISSPACE(CtrlStr[i])))
  563.             if (SpaceFlag) {
  564.             if (CtrlStr[i++] == SPACE_CHAR)
  565.                 fprintf(stderr, " ");
  566.             else
  567.                 fprintf(stderr, " %c", CtrlStr[i-1]);
  568.             SpaceFlag = FALSE;
  569.             }
  570.             else if (CtrlStr[i++] == SPACE_CHAR)
  571.             fprintf(stderr, " ");
  572.             else
  573.             fprintf(stderr, "%c", CtrlStr[i-1]);
  574.         while (!ISSPACE(CtrlStr[i]) && (i < strlen(CtrlStr))) {
  575.             if (CtrlStr[i] == '*') fprintf(stderr, "...");
  576.             i++;                 /* Skip the rest of it. */
  577.         }
  578.         fprintf(stderr, "]");
  579.         break;
  580.         case '!':
  581.         fprintf(stderr, " -%c", CtrlStr[i++]);
  582.         i += 2;             /* Skip the '%-' or '!- after the char! */
  583.         SpaceFlag = TRUE;
  584.         while (!ISCTRLCHAR(CtrlStr[i]) && (i < strlen(CtrlStr))    &&
  585.             (!ISSPACE(CtrlStr[i])))
  586.             if (SpaceFlag) {
  587.             if (CtrlStr[i++] == SPACE_CHAR)
  588.                 fprintf(stderr, " ");
  589.             else
  590.                 fprintf(stderr, " %c", CtrlStr[i-1]);
  591.             SpaceFlag = FALSE;
  592.             }
  593.             else if (CtrlStr[i++] == SPACE_CHAR)
  594.             fprintf(stderr, " ");
  595.             else
  596.                 fprintf(stderr, "%c", CtrlStr[i-1]);
  597.         while (!ISSPACE(CtrlStr[i]) && (i < strlen(CtrlStr))) {
  598.             if (CtrlStr[i] == '*') fprintf(stderr, "...");
  599.             i++;                 /* Skip the rest of it. */
  600.         }
  601.         break;
  602.         default:               /* Not checked, but must be last one! */
  603.         fprintf(stderr, " ");
  604.         while (!ISSPACE(CtrlStr[i]) && (i < strlen(CtrlStr)) &&
  605.                !ISCTRLCHAR(CtrlStr[i]))
  606.             fprintf(stderr, "%c", CtrlStr[i++]);
  607.         fprintf(stderr, "\n");
  608.         return;
  609.     }
  610.     }
  611.     fprintf(stderr, "\n");
  612. }
  613.  
  614. #ifdef    MYMALLOC
  615.  
  616. /***************************************************************************
  617. * My Routine to    allocate dynamic memory. All program requests must call    *
  618. * this routine (no direct call to malloc). Dies if no memory.           *
  619. ***************************************************************************/
  620. static char *MyMalloc(unsigned size)
  621. {
  622.     char *p;
  623.  
  624.     if ((p = (char *) malloc(size)) != NULL) return p;
  625.  
  626.     fprintf(stderr, "Not enough memory, exit.\n");
  627.     exit(2);
  628.  
  629.     return NULL;                    /* Makes warning silent. */
  630. }
  631.  
  632. #endif    /* MYMALLOC */
  633.